(function() {
  var crypto, express, iced, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  crypto = require('crypto');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 11
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
          });
          client.query("select * from web_email limit 1", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 15
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          (function(__iced_k) {
            if (result.rows.length === 0) {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
                });
                client.query("insert into web_email values ( default, '', 465, '', '', '', true, 3 )", __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 21
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                return __iced_k();
              });
            } else {
              return __iced_k();
            }
          })(function() {
            done();
            record = result.rows[0];
            return res.render('email', {
              record: record || {
                smtp_ssl: true
              }
            });
          });
        });
      };
    })(this));
  });

  router.post('/edit', function(req, res, next) {
    var cipher, client, done, err, k, passwd, result, secure_key, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['smtp_host', 'smtp_port', 'smtp_ssl', 'retry_cnt', 'smtp_from', 'smtp_name'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 33
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
          });
          client.query("update web_email set smtp_host = $1, smtp_port = $2, smtp_ssl = $3, retry_cnt = $4, smtp_from = $5, smtp_name = $6, notify_mail = $7, notify_level = $8, notify_recips = $9", [req.body.smtp_host, req.body.smtp_port, req.body.smtp_ssl, req.body.retry_cnt, req.body.smtp_from, req.body.smtp_name, req.body.notify_mail === 'true', parseInt(req.body.notify_level), req.body.notify_recips], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 49
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          (function(__iced_k) {
            if (req.body.smtp_passwd && req.body.smtp_passwd.length > 0) {
              secure_key = Buffer.from(settings.secure_key, 'hex');
              if (secure_key.length !== 16) {
                done();
                return next(new Error('安全密钥配置错误，必须由 32 位[0-9A-F]数字组成'));
              }
              cipher = crypto.createCipheriv('des-ede', secure_key, new Buffer(''));
              passwd = cipher.update(req.body.smtp_passwd, 'utf8', 'hex');
              passwd += cipher.final('hex');
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/email.coffee"
                });
                client.query("update web_email set smtp_passwd = $1", [passwd], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 64
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                return __iced_k();
              });
            } else {
              return __iced_k();
            }
          })(function() {
            done();
            return res.redirect('/email');
          });
        });
      };
    })(this));
  });

}).call(this);
